package lib.standard.f;

import com.acadsoc.base.httpretrofit.config.ConfigsForHttpretrofitSDK;
import com.acadsoc.base.util.DesUtil;

import java.util.Map;

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;

/**
 * FPlugin
 */
public class FPlugin implements FlutterPlugin, ServerApi.HostHttp {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
//    private MethodChannel channel;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        /*channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "f");
        channel.setMethodCallHandler(this);*/
        ServerApi.setup(flutterPluginBinding.getBinaryMessenger(), this);
    }

    /*@Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        LogUtils.e("调Android：" + call.method + ":参数:" + call.arguments());
        if (call.method.equals("logE")) {
            LogUtils.e(call.arguments());
        } else if (call.method.equals("signParams")) {
            String[] keyTrans = {"UserAccount", "Phone", "UserPwd"};
            for (String key : keyTrans) {
                if (call.hasArgument(key))
                    try {
                        ((Map) call.arguments()).put(key, URLEncoder.encode(DesUtil.encipherAES2Base64(call.argument(key).toString()), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
            }
            result.success(URLUtils.addSign((Map<String, String>) call.arguments()));
        } else {
            result.notImplemented();
        }
    }*/

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
//        channel.setMethodCallHandler(null);
        ServerApi.setup(binding.getBinaryMessenger(), null);

    }


    @NonNull
    @Override
    public Map sign(@NonNull Map arg) {

//        todo 补充其它需要加密的字段
        String[] keyTrans = {"UserAccount", "Phone", "UserPwd"};
        for (String key : keyTrans) {
            if (arg.containsKey(key))
                try {
                    arg.put(key, /*URLEncoder.encode(*/DesUtil.encipherAES2Base64(String.valueOf(arg.get(key)))/*, "UTF-8")*/);
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        return URLUtils.addSign(arg);
    }

    @NonNull
    @Override
    public String token() {
        return ConfigsForHttpretrofitSDK.getLoacalToken();
    }
}
